package com.xcc.java_juc;

import java.util.LinkedHashMap;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * synchronize的锁和解锁
 */
public class Demo02_WaitAwaitPark {

    static Object objectLock = new Object();
    static Lock lock = new ReentrantLock();
    static Condition condition = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
//        SynchroziedWaitNotify();

//        LockAwaitSignal();

        lockSupportParkUnpark();

    }

    private static void lockSupportParkUnpark() {
        Thread t1 = new Thread(() -> {
            try { Thread.sleep(3000); } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "=========进入");
            //如果这里有两个LockSupport.park(),因为permit的值为1,上一行已经使用了permit，
            // 所以下一行被注释的打开会导致程序处于一直等待的状态
            LockSupport.park();
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + "=========结束");
        }, "t1");
        t1.start();


        new Thread(() -> {
            //有两个LockSupport.unpark(t1),由于permit的值最大为1,所以只能给park一个通行证
            LockSupport.unpark(t1);
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName() + "=========唤醒");
        }, "t2").start();
    }

    private static void LockAwaitSignal() {
        new Thread(() -> {
            //如果把下行这句代码打开,先signal后await,会出现A线程一直处于等待状态
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //如果不加lock锁也会出现错误同synchronize
            lock.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "=========进入");
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
            System.out.println(Thread.currentThread().getName() + "=========结束");
        }, "t1").start();

        new Thread(() -> {
            lock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName() + "=========唤醒");
            } finally {
                lock.unlock();
            }
        }, "t2").start();
    }

    private static void SynchroziedWaitNotify() {
        new Thread(() -> {
            //如果注释掉，就会先执行进入程序等待被唤醒
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //如果注释掉synchronized 则会报错，因为wait和notify一定要在同步块或同步方法中
            synchronized (objectLock) {
                try {
                    System.out.println(Thread.currentThread().getName() + "=========进入");
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "=========结束");
            }
        }, "t1").start();


        new Thread(() -> {
            synchronized (objectLock) {
                objectLock.notify();
                System.out.println(Thread.currentThread().getName() + "=========唤醒");
            }
        }, "t2").start();
    }

}
